<?php

if (!defined('BASEPATH'))
    exit('Tidak Diperkenankan mengakses langsung');
/* Class  Model : customer
 * di Buat oleh Diar PHP Generator
 * Update List untuk grid karena program generatorku lom sempurna ya hehehehehe */

class modelcustomer extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function getArrayListcustomer() { /* spertinya perlu lock table */
        $xBuffResul = array();
        $xStr = "SELECT " .
                "idx," .
                "title," .
                "firstname," .
                "midlename," .
                "lastname," .
                "organization," .
                "position," .
                "address," .
                "idcity," .
                "idstate," .
                "idcountry," .
                "zipcode," .
                "telepon," .
                "mobile," .
                "fax," .
                "email," .
                "username," .
                "password," .
                "dateentry," .
                "idpegawai," .
                "status" .
                " FROM customer   order by idx ASC ";
        $query = $this->db->query($xStr);
        foreach ($query->result() as $row) {
            $xBuffResul[$row->idx] = $row->title;
        }
        return $xBuffResul;
    }

    function getListcustomer($xAwal, $xLimit, $xSearch = '') {
        if (!empty($xSearch)) {
            $xSearch = "Where title like '%" . $xSearch . "%' or firstname like '%" . $xSearch . "%' or midlename like '%" . $xSearch . "%' or lastname like '%" . $xSearch . "%'";
        }
        $xStr = "SELECT " .
                "idx," .
                "title," .
                "firstname," .
                "midlename," .
                "lastname," .
                "organization," .
                "position," .
                "address," .
                "idcity," .
                "idstate," .
                "idcountry," .
                "zipcode," .
                "telepon," .
                "mobile," .
                "fax," .
                "email," .
                "username," .
                "password," .
                "dateentry," .
                "idpegawai," .
                "status" .
                " FROM customer $xSearch order by idx DESC limit " . $xAwal . "," . $xLimit;
        $query = $this->db->query($xStr);
        return $query;
    }
    function getListcustomerbydate($xAwal, $xLimit, $xSearch = '',$date_mulai='NOW()',$date_akhir='NOW()',$xidstatus) {
        
             $xSearch = "Where CONCAT_WS(' ',firstname,midlename,lastname) like '%" . $xSearch . "%' AND status='".$xidstatus."' " .
                       "  AND (idx in (SELECT idcustomer FROM historibayar WHERE date_bayar >= '".$date_mulai."' AND date_bayar <= '".$date_akhir."')) ";
       
        $xStr = "SELECT " .
                "idx," .
                "title," .
                "firstname," .
                "midlename," .
                "lastname," .
                "organization," .
                "position," .
                "address," .
                "idcity," .
                "idstate," .
                "idcountry," .
                "zipcode," .
                "telepon," .
                "mobile," .
                "fax," .
                "email," .
                "username," .
                "password," .
                "dateentry," .
                "idpegawai," .
                "status" .
                " FROM customer $xSearch order by idx DESC limit " . $xAwal . "," . $xLimit;
        $query = $this->db->query($xStr);
        return $query;
    }

    function getDetailcustomer($xidx) {
        $xStr = "SELECT " .
                "idx," .
                "title," .
                "firstname," .
                "midlename," .
                "lastname," .
                "organization," .
                "position," .
                "address," .
                "idcity," .
                "idstate," .
                "idcountry," .
                "zipcode," .
                "telepon," .
                "mobile," .
                "fax," .
                "email," .
                "username," .
                "password," .
                "dateentry," .
                "idpegawai," .
                "status" .
                " FROM customer  WHERE idx = '" . $xidx . "'";

        $query = $this->db->query($xStr);
        $row = $query->row();
        return $row;
    }
    function getDetailnamacustomer($xidx) {
        $xStr = "SELECT " .
                "idx," .
                "title," .
                "firstname," .
                "midlename," .
                "lastname," .
                "organization," .
                "position," .
                "address," .
                "idcity," .
                "idstate," .
                "idcountry," .
                "zipcode," .
                "telepon," .
                "mobile," .
                "fax," .
                "email," .
                "username," .
                "password," .
                "dateentry," .
                "idpegawai," .
                "status" .
                " FROM customer  WHERE idx = '" . $xidx . "'";

        $query = $this->db->query($xStr);
        $row = $query->row();
        return $row->title .' '. $row->firstname.' '.$row->midlename.' '.$row->lastname;
    }

    function getLastIndexcustomer() { /* spertinya perlu lock table */
        $xStr = "SELECT " .
                "idx," .
                "title," .
                "firstname," .
                "midlename," .
                "lastname," .
                "organization," .
                "position," .
                "address," .
                "idcity," .
                "idstate," .
                "idcountry," .
                "zipcode," .
                "telepon," .
                "mobile," .
                "fax," .
                "email," .
                "username," .
                "password," .
                "dateentry," .
                "idpegawai," .
                "status" .
                " FROM customer order by idx DESC limit 1 ";
        $query = $this->db->query($xStr);
        $row = $query->row();
        return $row;
    }

    Function setInsertcustomer($xidx, $xtitle, $xfirstname, $xmidlename, $xlastname, $xorganization, $xposition, $xaddress, $xidcity, $xidstate, $xidcountry, $xzipcode, $xtelepon, $xmobile, $xfax, $xemail, $xusername, $xpassword, $xdateentry, $xidpegawai, $xstatus) {
        $xStr = " INSERT INTO customer( " .
                "idx," .
                "title," .
                "firstname," .
                "midlename," .
                "lastname," .
                "organization," .
                "position," .
                "address," .
                "idcity," .
                "idstate," .
                "idcountry," .
                "zipcode," .
                "telepon," .
                "mobile," .
                "fax," .
                "email," .
                "username," .
                "password," .
                "dateentry," .
                "idpegawai," .
                "status) VALUES('" . $xidx . "','" . $xtitle . "','" . $xfirstname . "','" . $xmidlename . "','" . $xlastname . "','" . $xorganization . "','" . $xposition . "','" . $xaddress . "','" . $xidcity . "','" . $xidstate . "','" . $xidcountry . "','" . $xzipcode . "','" . $xtelepon . "','" . $xmobile . "','" . $xfax . "','" . $xemail . "','" . $xusername . "','" . $xpassword . "',now(),'" . $xidpegawai . "','" . $xstatus . "')";
        $query = $this->db->query($xStr);
        return $xidx;
    }

    Function setUpdatecustomer($xidx, $xtitle, $xfirstname, $xmidlename, $xlastname, $xorganization, $xposition, $xaddress, $xidcity, $xidstate, $xidcountry, $xzipcode, $xtelepon, $xmobile, $xfax, $xemail, $xusername, $xpassword, $xdateentry, $xidpegawai, $xstatus) {
        $xStr = " UPDATE customer SET " .
                "idx='" . $xidx . "'," .
                "title='" . $xtitle . "'," .
                "firstname='" . $xfirstname . "'," .
                "midlename='" . $xmidlename . "'," .
                "lastname='" . $xlastname . "'," .
                "organization='" . $xorganization . "'," .
                "position='" . $xposition . "'," .
                "address='" . $xaddress . "'," .
                "idcity='" . $xidcity . "'," .
                "idstate='" . $xidstate . "'," .
                "idcountry='" . $xidcountry . "'," .
                "zipcode='" . $xzipcode . "'," .
                "telepon='" . $xtelepon . "'," .
                "mobile='" . $xmobile . "'," .
                "fax='" . $xfax . "'," .
                "email='" . $xemail . "'," .
                "username='" . $xusername . "'," .
                "password='" . $xpassword . "'," .
//                "dateentry='" . $xdateentry . "'," .
                "idpegawai='" . $xidpegawai . "'," .
                "status='" . $xstatus . "' WHERE idx = '" . $xidx . "'";
        $query = $this->db->query($xStr);
        return $xidx;
    }

    function setDeletecustomer($xidx) {
        $xStr = " DELETE FROM customer WHERE customer.idx = '" . $xidx . "'";

        $query = $this->db->query($xStr);
        $this->setInsertLogDeletecustomer($xidx);
    }

    function setInsertLogDeletecustomer($xidx) {
        $xidpegawai = $this->session->userdata('idpegawai');
        $xStr = "insert into logdelrecord(idxhapus,nmtable,tgllog,ideksekusi) values($xidx,'customer',now(),$xidpegawai)";
        $query = $this->db->query($xStr);
    }

}

?>